//
//  StrUtils.m
//  PalmHospitalTongJi
//
//  Created by mardin partytime on 13-11-28.
//  Copyright (c) 2013年 lvxian. All rights reserved.
//

#import "StrUtils.h"

@implementation StrUtils

+ (NSString *)distance:(NSString *)aDis
{
    if (aDis == nil || [aDis isEqualToString:@""]) {
        return @"";
    }
    NSString *result;
    double dis_l = [aDis doubleValue];
    if (dis_l < 1000.0) {
        result = [[NSString alloc] initWithFormat:@"%@米",aDis];
    }else {
        int tmp =  (int)dis_l / 100;
        result = [[NSString alloc] initWithFormat:@"%d.%d千米",tmp/10, tmp%10];
    }
    
    return result;
}

+ (NSString *)timeParse:(NSString *)aStr
{
    if (aStr == nil || [aStr isEqualToString:@""]) {
        return aStr;
    }
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [format dateFromString:aStr];
    
    NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
    [newFormat setDateFormat:@"yyyy'年'MM'月'dd'日'"];
    NSString *newStr = [newFormat stringFromDate:date];
    return newStr;
}

+ (NSString *)timeNumToStr:(NSString *)aStr
{
    if (aStr == nil || [aStr isEqualToString:@""]) {
        return aStr;
    }
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy'-'MM'-'dd"];
    NSDate *date = [format dateFromString:aStr];
    
    NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
    [newFormat setDateFormat:@"yyyy'年'MM'月'dd'日'"];
    NSString *newStr = [newFormat stringFromDate:date];
    
    NSArray *dayArray = [[NSArray alloc] initWithObjects:  @"周日",@"周一", @"周二", @"周三", @"周四", @"周五", @"周六",nil];
    [newFormat setDateFormat:@"c"];
    NSString *day_l = (NSString *)[dayArray objectAtIndex:([[newFormat stringFromDate:date] integerValue]) - 1];
    
    NSString *resultStr = [[NSString alloc] initWithFormat:@"%@.%@", newStr, day_l];
    
    return resultStr;
}



+ (NSString *)timeNumToBubbleStr:(NSString *)aStr
{
    if (aStr == nil || [aStr isEqualToString:@""]) {
        return aStr;
    }
    
    NSArray *dayArray = [[NSArray alloc] initWithObjects:  @"周日",@"周一", @"周二", @"周三", @"周四", @"周五", @"周六",nil];
    //   NSDictionary *dayDic = [[NSDictionary alloc] initWithObjects:<#(NSArray *)#> forKeys:<#(NSArray *)#>];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy'-'MM'-'dd"];
    NSDate *date = [format dateFromString:aStr];
    
    
    
    NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
    
    [newFormat setDateFormat:@"c"];
    NSString *day_l = (NSString *)[dayArray objectAtIndex:([[newFormat stringFromDate:date] integerValue]) - 1];
    
    [newFormat setDateFormat:@"MM'/'dd"];
    NSString *newStr = [newFormat stringFromDate:date];
    NSString *resultStr = [[NSString alloc] initWithFormat:@"%@.%@",newStr, day_l];
    return resultStr;
}

+ (NSString *)priceParse:(NSNumber *)aNumber
{
    NSString *price = nil;
    if([aNumber intValue] != 0){
        float num = [aNumber floatValue];
        price = [NSString stringWithFormat:@"%.1f",(num / 100.0)];
    }
    return price;
    
}

+ (NSString *)commentTime:(NSString *)aStr
{
    NSArray *array_l = [aStr componentsSeparatedByString:@" "];
    if (array_l != nil && array_l.count > 0) {
        //    NSLog(@"time:%@", [array_l objectAtIndex:0]);
        return [array_l objectAtIndex:0];
    }
    return @"";
}

+(NSInteger)getAgeFromId:(NSString *)aStr
{
    if (aStr == nil && [aStr isEqualToString:@""]) {
        return 0;
    }
    
    NSString *ageStr_l;
    if (aStr.length == 15) {
        NSRange range_l = {6,2};
        NSString *time_l = [aStr substringWithRange:range_l];
        ageStr_l = [[NSString alloc] initWithFormat:@"19%@",time_l];
    }else if (aStr.length == 18) {
        NSRange range_l = {6,4};
        ageStr_l = [aStr substringWithRange:range_l];
    }
    
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy"];
    NSString *nowYear_l = [format stringFromDate:[NSDate date]];
    
    NSInteger age = [nowYear_l integerValue] - [ageStr_l integerValue];
    return age;
}

+ (NSInteger)getGenderFromId:(NSString *)aStr
{
    if (aStr == nil && [aStr isEqualToString:@""]) {
        return 0;
    }
    if (aStr.length == 15) {
        NSRange range_l = {14,1};
        NSString *bit_l = [aStr substringWithRange:range_l];
        if ( bit_l.integerValue % 2 == 0 ) {
            return 2;
        }else {
            return 1;
        }
    }else if (aStr.length == 18) {
        NSRange range_l = {16,1};
        NSString *bit_l = [aStr substringWithRange:range_l];
        if ( bit_l.integerValue % 2 == 0 ) {
            return 2;
        }else {
            return 1;
        }
    }
    return 0;
}

+ (BOOL)checkPwd:(NSString *)aStr
{
    if (aStr == nil) {
        return NO;
    }
    
    if (aStr.length >= 6 && aStr.length <= 16) {
        return YES;
    }else
        return NO;
}

+ (BOOL)checkMobileNumber:(NSString *)aStr
{
    if (aStr == nil) {
        return NO;
    }
    NSString * mobileFormat = @"^((13[0-9])|(15[^(4,\\D)])|(18[0-9]))\\d{8}$";
    
    NSPredicate *regexMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileFormat];
    if ([regexMobile evaluateWithObject:aStr]) {
        return YES;
    }else {
        return NO;
    }
}

+ (BOOL)isValidateIDCard:(NSString *)IDCard
{
    //    NSString *IDCardRegex15 = @"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
    //    NSString *IDCardRegex18 = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}[\\d|x|X]$";
    //    NSPredicate *predicate15 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", IDCardRegex15];
    //    NSPredicate *predicate18 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", IDCardRegex18];
    //    return [predicate15 evaluateWithObject:IDCard] || [predicate18 evaluateWithObject:IDCard];
    return [self checkIdentity:IDCard];
}

/// <summary>
/// 验证身份证号码
/// </summary>
/// <param name="Id">身份证号码</param>
/// <returns>验证成功为YES，否则为NO</returns>
+ (BOOL)checkIdentity:(NSString *)cardId
{
    NSString* checkId = cardId;
    if(cardId.length == 15){
        checkId = [self convertCard15To18:cardId];
        return [self CheckIDCard18:checkId];
    }
    else if(cardId.length == 18){
        return [self CheckIDCard18:cardId];
    }
    return NO;
}


//验证18位身份证号码
+ (BOOL)CheckIDCard18:(NSString*)cardId
{
    NSArray* address =  @[@"11",@"22",@"35",@"44",@"53",@"12",@"23",@"36",@"45",
                          @"54",@"13",@"31",@"37",@"46",@"61",@"14",@"32",@"41",
                          @"50",@"62",@"15",@"33",@"42",@"51",@"63",@"21",@"34",
                          @"43",@"52",@"64",@"65",@"71",@"81",@"82",@"91"];
    for( int i = 0; i < [address count]; ++i){
        if ([[cardId substringWithRange:NSMakeRange(0, 2)] isEqualToString:address[i]]){
            break;//省份验证正确
        }
        else if (i == [address count] - 1){
            return false;//省份验证错误
        }
    }
    
    NSString* birth = [cardId substringWithRange:NSMakeRange(6, 8)];
    if(![self CheckDate:[birth substringWithRange:NSMakeRange(0, 4)]
                         month:[birth substringWithRange:NSMakeRange(4, 2)]
                           day:[birth substringWithRange:NSMakeRange(6, 2)]]){
        return NO;//生日验证错误
    }
    if(![self VarifyCode:cardId]){
        return NO;//最后一位校验错误
    }
    return YES;
}

//实现身份证的15位转18位
+ (NSString*)convertCard15To18:(NSString*)cardId
{
    if(cardId.length != 15){
        return @"";
    }
    int iS = 0;
    
    //加权因子常数
    NSArray* iW = @[@7, @9, @10, @5, @8, @4, @2, @1, @6, @3, @7, @9, @10, @5, @8, @4, @2];
    //校验码常数
    NSString* LastCode = @"10X98765432";
    //新身份证号
    NSMutableString* idNew = [[NSMutableString alloc] init];
    [idNew appendString:[cardId substringWithRange:NSMakeRange(0, 6)]];
    //填在第6位及第7位上填上‘1’，‘9’两个数字
    [idNew appendString:@"19"];
    [idNew appendString:[cardId substringWithRange:NSMakeRange(6, 9)]];
    for(int i = 0; i < idNew.length && i < [iW count]; ++i){
        iS += ([[idNew substringWithRange:NSMakeRange(i, 1)] intValue] * [[iW objectAtIndex:i] intValue]);
    }
    //取模运算，得到模值
    int iY = iS % 11;
    //从LastCode中取得以模为索引号的值，加到身份证的最后一位，即为新身份证号。
    [idNew appendString:[LastCode substringWithRange:NSMakeRange(iY, 1)]];
    return idNew;
}


+ (BOOL)CheckDate:(NSString*)year month:(NSString*)month day:(NSString*)day
{
    int iYear  = [year intValue];
    int iMonth = [month intValue];
    int iDay = [day intValue];
    NSMutableArray* Days = [NSMutableArray arrayWithObjects:@31,@28,@31,@30,@31,@30,@31,@31,@30,@31,@30,@31,nil];
    if(iMonth < 1 || iMonth>12)
        return   NO;
    
    BOOL b_IsLeapYear = NO;
    if(iYear % 4 == 0){
        b_IsLeapYear = YES;
        if(!(iYear % 100 == 0 && iYear % 400 == 0))
            b_IsLeapYear = NO;
    }
    
    if(b_IsLeapYear){
        [Days replaceObjectAtIndex:1 withObject:@29];
    }
    else{
        [Days replaceObjectAtIndex:1 withObject:@28];
    }
    
    if(iDay < 0 || iDay > [[Days objectAtIndex:iMonth - 1] intValue])
        return NO;
    
    return YES;
}

+ (BOOL)VarifyCode:(NSString*)cardId
{
    int iS = 0;
    //加权因子常数
    NSArray* iW = @[@7, @9, @10, @5, @8, @4, @2, @1, @6, @3, @7, @9, @10, @5, @8, @4, @2];
    //校验码常数
    NSString* LastCode = @"10X98765432";
    
    for(int i = 0;i < 17;i++){
        iS += ([[cardId substringWithRange:NSMakeRange(i, 1)] intValue] * [[iW objectAtIndex:i] intValue]);
    }
    
    //取模运算，得到模值
    int iY = iS % 11;
    if(iY < [LastCode length]){
        NSString* lastChar = [LastCode substringWithRange:NSMakeRange(iY, 1)];
        if([lastChar isEqualToString:[cardId substringFromIndex:cardId.length - 1]]){
            return YES;
        }
    }
    return NO;
}

+ (BOOL)checkEmail:(NSString *)aStr
{
    if (aStr == nil) {
        return NO;
    }
//    NSString *emailFormat = @"^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
    NSString *emailFormat = @"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    NSPredicate *regexEmail = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailFormat];
    if ([regexEmail evaluateWithObject:aStr]) {
        return YES;
    }else {
        return NO;
    }
    
}

+ (BOOL)isEmpty:(NSString *)aStr
{
    if (aStr == nil || [aStr isEqual:[NSNull null]] || aStr.length == 0 || [aStr isEqualToString:@""]) {
        return YES;
    }
    
    return NO;
}

+ (NSString*)NilToEmptyString:(NSString*)str
{
    return str ? str : @"";
}

+ (NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    
    return html;
}

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

+ (NSString *)formatterDate:(NSString *)datetime dateform:(NSString*)dateform{
        double dateNumber = [[NSString stringWithFormat:@"%@", datetime] doubleValue] / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateNumber];
        NSDateFormatter *formatter = [NSDateFormatter new];
        [formatter setDateFormat:dateform];
        [formatter setTimeZone:[NSTimeZone localTimeZone]];
        return [formatter stringFromDate:date];
}

+(NSMutableAttributedString*)updateStringColor:(NSString*)string begNum:(NSInteger)begNum endNum:(NSInteger)endNum color:(UIColor*)color{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
    [str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(begNum,endNum)];
    return str;
}

+(NSString*)formatterDate:(NSString*)datetime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date=[formatter dateFromString:datetime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    return currentDateStr;
}

+(NSString*)getTimeString{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970]*1000;
    NSString *timeString = [NSString stringWithFormat:@"%0.f", a];
    return timeString;
}

+(CGRect)getStrongTextFontLength:(NSString*)str atFontSize:(NSInteger)fontSize atContainerWidth:(float)width{
   NSAttributedString *name = [[NSAttributedString alloc]initWithString:str attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}];
    CGRect nameRect = [name boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    return nameRect;
}

+(CGSize)getStrongWidth:(NSString*)str atFont:(UIFont*)font{
   CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:font}];
    return size;
}

+(CGSize)getStrongTextFontLength:(NSString*)str atFontSize:(NSInteger)size{
    
    CGSize fontSize = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    return fontSize;
}

+(void)NsLogError:(NSError*)error{
    if (error) {
        NSLog(@"错误:%@",error);
    }
}

+ (NSString *)timeFormatted:(long)totalSeconds
{
    
    long seconds = totalSeconds % 60;
    long minutes = (totalSeconds / 60) % 60;
    long hours = totalSeconds / 3600;
    
    if (hours == 0) {
        return [NSString stringWithFormat:@"%02ld分%02ld秒", minutes, seconds];
    }else{
        return [NSString stringWithFormat:@"%02ld小时%02ld分%02ld秒",hours, minutes, seconds];
    }
}

+(NSString *) compareCurrentTime:(NSDate*) compareDate
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}

+ (NSString *) getUrlParams:(NSString *) url withParam:(NSString *)param{
    
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",param];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    // 执行匹配的过程
    NSArray *matches = [regex matchesInString:url
                                      options:0
                                        range:NSMakeRange(0, [url length])];
    for (NSTextCheckingResult *match in matches) {
        NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
        return tagValue;
    }
    return nil;
}

+(NSString*)getNSDictionaryValue:(NSDictionary*)dict atFind:(NSString*)str{
    NSString* findStr = @"";
    for (id key in dict) {
        if ([key isEqualToString:str]) {
            findStr = [dict objectForKey:str];
        }
    }
    return findStr;
}
@end
